﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Ue.Infrastructure.DomainBase;
using Ue.Infrastructure.RepositoryFramework.Repository;
using Ue.Qcdz.Domain;
using Ue.Qcdz.Domain.FriendLink;
using Ue.Qcdz.UI.Controls.FriendLink;

namespace Ue.Qcdz.Repositories.FriendLink
{
    public class FriensdLinkRepository : SqlServerRepository<FriendLinks>, IFriendLinksRepository
    {
        #region Overrides of SqlServerRepositoryBase<FriendLinks>

        protected override string GetDataTableName()
        {
            return "FriendLink";
        }
        protected override string GetKeyFieldName()
        {
            return FriendLinkFactory.FieldNames.EntityKey;
        }
        protected override string[] GetBaseInsertFields()
        {
            return new string[]
                       {
                        FriendLinkFactory.FieldNames.EntityKey,
                        FriendLinkFactory.FieldNames.LinkName,
                        FriendLinkFactory.FieldNames.LinkUrl,
                        FriendLinkFactory.FieldNames.Remark,
                        FriendLinkFactory.FieldNames.Status
        };
        }

        protected override string[] GetBaseUpdateFields()
        {
            return new string[]
                       {
                        FriendLinkFactory.FieldNames.LinkName,
                        FriendLinkFactory.FieldNames.LinkUrl,
                        FriendLinkFactory.FieldNames.Remark,
                        FriendLinkFactory.FieldNames.Status
        };
        }

        protected override IDataParameter[] BuildParameters(FriendLinks entity)
        {
            return new IDataParameter[]
                       {
                            RepositoryDriver.BuildParameter(FriendLinkFactory.FieldNames.EntityKey,entity.Key),
                            RepositoryDriver.BuildParameter(FriendLinkFactory.FieldNames.LinkName,entity.LinkName),
                            RepositoryDriver.BuildParameter(FriendLinkFactory.FieldNames.LinkUrl,entity.LinkUrl),
                            RepositoryDriver.BuildParameter(FriendLinkFactory.FieldNames.Remark,entity.Remark),
                            RepositoryDriver.BuildParameter(FriendLinkFactory.FieldNames.Status,entity.Status)
                       };
        }

        protected override IWhereClause BuildWhereClauseBy(IDataQuery dataQuery)
        {
            if(dataQuery!=null)
            {
                FileLinkView fileLinkView = dataQuery as FileLinkView;
                return new LinkViewClause(fileLinkView);
            }
            return null;
        }

        protected override IDictionary<string, ChildDataHandler> GetBuildEntityChildCallbacks()
        {
            return null;
        }
        public class LinkViewClause : IWhereClause
        {
            #region Implementation of IWhereClause

            public FileLinkView friendLinkView;
            public LinkViewClause(FileLinkView friendLinkView)
            {
                this.friendLinkView = friendLinkView;
            }
            public string Clause
            {
                get
                {
                    var clause = "LinkName LIKE '%'+@LinkName+'%' AND Status LIKE '%'+@Status+'%'";
                    return clause;
                }
            }

            public IDataParameter[] Parameters
            {
                get
                {
                    if(string.IsNullOrEmpty(friendLinkView.SearchKey))friendLinkView.SearchKey="";
                    if (string.IsNullOrEmpty(friendLinkView.Status)) friendLinkView.Status = "";
                    var Parameters = new IDataParameter[]
                                         { 
                                             SqlServerDriver.Instance.BuildParameter(FriendLinkFactory.FieldNames.LinkName,
                                                                                     friendLinkView.SearchKey),
                                             SqlServerDriver.Instance.BuildParameter(FriendLinkFactory.FieldNames.Status,
                                                                                     friendLinkView.Status)
                };
                    return Parameters;
                }
            }

            #endregion
        }
        #endregion

        #region Implementation of IFriendLinksRepository

        public IList<FriendLinks> FindBy(FriendListEnum status,int num)
        {
            num = num==0 ? 999 : num;
            var query = string.Format("SELECT TOP {0} * FROM FriendLink WHERE status=@status", num);
            var parameter = new IDataParameter[]
                                {
                                    this.RepositoryDriver.BuildParameter(FriendLinkFactory.FieldNames.Status,status)
                                };
            return this.BuildEntitiesFromSql(query, parameter);
        }

        #endregion
    }
}
